package com.hexacta.hrs.test.data;

import static com.hexacta.hrs.test.data.DomainTestDataDependencyCreator.HEXACTA_DOMAIN;
import static com.hexacta.hrs.test.data.PersonTestDataDependencyCreator.ADMIN_PERSON;

import java.util.List;
import java.util.Map;

import org.vectrics.domain.Domain;
import org.vectrics.user.Person;
import org.vectrics.user.User;
import org.vectrics.user.UserFactory;

/**
 * Test data dependency creator for the User class.
 * 
 * @author tpollak
 */
public class UserTestDataDependencyCreator extends AbstractTestDataDependencyCreator {

    public static final String ADMIN_USER = "ADMIN_USER";

    public UserTestDataDependencyCreator(List<TestDataDependencyCreator> dependencies) {
        super(dependencies);
    }

    public void createDependentTestData(Map<String, Object> context) {
        User user = this.createUser(context, "admin", "hexacta");

        user = this.insertDomainObject(user);

        this.storeObjectReferenceInContext(context, ADMIN_USER, user);
    }

    private User createUser(Map<String, Object> context, String username, String password) {
        Domain hexactaDomain = this.loadObjectFromContext(context, HEXACTA_DOMAIN);
        Person admin = this.loadObjectFromContext(context, ADMIN_PERSON);
        User user = UserFactory.createUser();
        user.setDomain(hexactaDomain);
        user.setCreatedBy(admin);
        user.setCreatedDate(this.getDataUtils().createCurrentDate());
        user.setLastModifiedBy(admin);
        user.setLastModifiedDate(this.getDataUtils().createCurrentDate());
        user.setPerson(admin);
        user.setUserName(username);
        user.setPassword(password);
        user.setPasswordEncrypted(false);

        return user;
    }

}
